package com.lins.framework.common.base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

import com.lins.framework.utils.tip.ToastUtil;

import java.lang.ref.WeakReference;

/**
 * Created by Admin on 2017/3/13.
 */

public class BaseActivity extends AppCompatActivity implements BaseFuncIml {
    public Handler handler;
    private int mFragmentId;
    protected Fragment mCurrentFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ((MyApplication) this.getApplication()).getActivityManager()
                .pushActivity(this); // 将activity推入管理栈
        super.onCreate(savedInstanceState);
        handler = new BaseHandler(this);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        initData();
        initView();
        initListener();
        initLoad();
    }

    @Override
    protected void onDestroy() { // 销毁时activity出栈
        ((MyApplication) this.getApplication()).getActivityManager()
                .popActivity(this);
        super.onDestroy();
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initLoad() {

    }

    @Override
    public void handleMessage(Message msg) {

    }

    public void setFragmentId(int fragmentId) {
        this.mFragmentId = fragmentId;
    }

    public void setCurrentFragment(Fragment currentFragment) {
        this.mCurrentFragment = currentFragment;
    }

    protected void toFragment(Fragment toFragment) {
        if (mCurrentFragment == null) {
            ToastUtil.showToast(this, "mCurrentFragment is null");
            return;
        }
        if (toFragment == null) {
            ToastUtil.showToast(this, "toFragment is null");
            return;
        }
        if (toFragment.isAdded()) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .hide(mCurrentFragment)
                    .show(toFragment)
                    .commit();
        } else {
            getSupportFragmentManager()
                    .beginTransaction()
                    .hide(mCurrentFragment)
                    .add(mFragmentId, toFragment)
                    .show(toFragment)
                    .commit();
        }
    }

    protected void gotoActivity(Class<? extends BaseActivity> activity) {
        gotoActivity(activity, null);
    }

    protected void gotoActivity(Class<? extends BaseActivity> activity, Bundle bundle) {
        Intent intent = new Intent(this, activity);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 在程序消息队列中排队的消息保持了对目标Handler类的应用。如果Handler是个内部类，那么它也会保持它所在的外部类的引用。
     * 为了避免泄露这个外部类，应该将Handler声明为static嵌套类，并且使用对外部类的弱应用。
     */
    static class BaseHandler
            extends Handler {
        WeakReference<BaseActivity> mActivity;

        BaseHandler(BaseActivity activity) {
            mActivity = new WeakReference<BaseActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            BaseActivity activity = mActivity.get();
            super.handleMessage(msg);
            activity.handleMessage(msg);
        }
    }
}

